﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.ServiceModel.Syndication;
using System.ServiceModel.Web;
using System.Text;
using System.Configuration;

namespace TfsRssService
{
    public class BuildFeed : IBuildFeed
    {
        [OperationBehavior(Impersonation = ImpersonationOption.Required)]
        public SyndicationFeedFormatter CreateFeed(string projCollection, string proj)
        {
            // Create a new Syndication Feed.
            SyndicationFeed feed = new SyndicationFeed("Builds", "Recent builds for " + projCollection + @"/" + proj, null);
            List<SyndicationItem> items = new List<SyndicationItem>();

            // Create a new Syndication Item.
            SyndicationItem item = new SyndicationItem("An item", "Item content", new Uri(@"http://www.bbc.co.uk"));
            items.Add(item);
            feed.Items = items;

            // Return ATOM or RSS based on query string
            // rss -> http://localhost:8732/Builds/testCollection/testProject/
            // atom -> http://localhost:8732/Builds/testCollection/testProject/?format=atom
            string query = WebOperationContext.Current.IncomingRequest.UriTemplateMatch.QueryParameters["format"];
            SyndicationFeedFormatter formatter = null;
            if (query == "atom")
            {
                formatter = new Atom10FeedFormatter(feed);
            }
            else
            {
                formatter = new Rss20FeedFormatter(feed);
            }

            return formatter;
        }
    }
}
